@file:Suppress("DEPRECATION")

package com.evendai.sipdemo.util

import android.app.Activity
import android.app.Dialog
import android.app.ProgressDialog
import com.evendai.loglibrary.Timber
import org.jetbrains.anko.alert
import org.jetbrains.anko.indeterminateProgressDialog
import org.jetbrains.anko.progressDialog

@Suppress("MemberVisibilityCanBePrivate", "unused")
object DialogUtil {

    private var mLoadingDialog: ProgressDialog? = null
    private val dialogs = ArrayList<Dialog>()

    fun cancelAllDialogs() {
        val listIterator = dialogs.listIterator()
        while (listIterator.hasNext()) {
            val next = listIterator.next()
            if (next.isShowing) {
                try { next.cancel() } catch (e: Exception) { Timber.e(e,"取消Dialog时出现异常") } // 在Activity被销毁时取消Dialog会出现异常的
            }
            listIterator.remove()
        }
    }

    fun addDialog(dialog: Dialog) {
        if (!dialogs.contains(dialog)) {
            dialogs.add(dialog)
        }
    }

    fun showDialog(activity: Activity, msg: String, title: String = "提示", confirmButtonDesc: String = activity.resources.getString(android.R.string.yes), onClickConfirmButton: (()->Unit)? = null) {
        cancelAllDialogs()
        if (!activity.isFinishing) {
            val mTipDialog = activity.alert(msg, title) {
                positiveButton(confirmButtonDesc) {
                    onClickConfirmButton?.invoke()
                    cancelAllDialogs()
                }
            }
            mTipDialog.isCancelable = false
            addDialog(mTipDialog.show())
        }
    }

    fun showDialog(activity: Activity, msg: String, title: String = "提示", confirmButtonDes: String = "确定", onClickConfirmButton: (()->Unit)? = null, cancelButtonDesc: String = "取消", onClickCancelButton: (()->Unit)? = null) {
        cancelAllDialogs()
        if (!activity.isFinishing) {
            val mTipDialog = activity.alert(msg, title) {
                positiveButton(confirmButtonDes) {
                    onClickConfirmButton?.invoke()
                    cancelAllDialogs()
                }
                negativeButton(cancelButtonDesc) {
                    onClickCancelButton?.invoke()
                    cancelAllDialogs()
                }
            }
            mTipDialog.isCancelable = false
            addDialog(mTipDialog.show())
        }
    }

    fun showResultDialog(activity: Activity, msg: String, onClickYesButton: (()->Unit)? = null) {
        showDialog(activity, msg, "请求结果：", onClickConfirmButton = onClickYesButton)
    }

    fun showProgressDialog(activity: Activity, max: Int, msg: String = "正在下载apk：", title: String = "提示"): ProgressDialog? {
        cancelAllDialogs()
        if (!activity.isFinishing) {
            val dialog = activity.progressDialog(msg, title) { this.max = max }
            dialog.setProgressNumberFormat("0.00M/0.00M")
            dialog.setCancelable(false)
            addDialog(dialog)
            return dialog
        }
        return null
    }

    fun showLoading(activity: Activity) {
        if (!activity.isFinishing) {
            hideLoading()
            mLoadingDialog = activity.indeterminateProgressDialog(message = "请稍等。。。", title = "提示")
            mLoadingDialog!!.setCancelable(false)
            mLoadingDialog!!.show()
        }
    }

    fun hideLoading() {
        if (mLoadingDialog != null && mLoadingDialog!!.isShowing) {
            mLoadingDialog!!.cancel()
            mLoadingDialog = null
        }
    }
}